/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.alxbrendmaquinarias.controller.operation;

import com.alxbrendmaquinarias.model.InputDocument;
import com.alxbrendmaquinarias.service.InputDocumentService;
import com.alxbrendmaquinarias.service.ProductShopService;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.NamedQuery;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 *
 * @author Max
 */
@Controller
public class DocumentController implements Serializable{
    
    @Autowired
    InputDocumentService inputDocumentService;
    @Autowired
    ProductShopService productShopService;
    
    private LazyDataModel<InputDocument> listInputDocument;
    private InputDocument inputDocument;
    private Date today;
    private String namedQueryInputDocument = "InputDocument.findAllID";
    private String queryCount = "SELECT count(p) FROM InputDocument p";
    private String searchNumberDocument = "";
    private Date searchDate = null;

    public void initValuesForInputDocument() {
        listInputDocument = null;
        today = new Date();
        searchNumberDocument = "";
        searchDate = null;
        queryCount = "SELECT count(p) FROM InputDocument p";
        System.out.println("Entrando al inicio");
    }
    
    public void inializeInputDocument(Integer id) throws Exception {
        inputDocument = inputDocumentService.getByIdLazyDetail(id);
        System.out.println("Input: " + inputDocument);
        System.out.println("Lista: " + inputDocument.getDetailInputDocumentList());
    }
    
    public LazyDataModel<InputDocument> getListInputDocument() throws Exception {
        System.out.println("Llamdno al geter");
        if(listInputDocument == null) {
            System.out.println("Entraod al nulo");
            listInputDocument = new LazyDataModel<InputDocument>() {
                @Override
                public List<InputDocument> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
                    //return super.load(first, pageSize, sortField, sortOrder, filters); //To change body of generated methods, choose Tools | Templates.
                    System.out.println("Entrado a lazy");
                    List<InputDocument> lazyPS;
                    try {
                        getParameters();
                        listInputDocument.setRowCount(inputDocumentService.count(queryCount, getParameters()));
                        lazyPS = productShopService.listLazy(namedQueryInputDocument, getParameters(), first, pageSize);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                                
                        lazyPS = new ArrayList<>();
                        //Logger.getLogger(InputProductController.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    return lazyPS;
                }
                
                
            };
            
            //listInputDocument.setRowCount(productShopService.count("ProductShop.count", getParameters()));
                    //inputDocumentService.list("InputDocument.findAllID");
        }
        return listInputDocument;
    }

    public Map<String, Object> getParameters() {
        Map<String, Object> param;
        queryCount = "SELECT count(i) from InputDocument i";
        if(searchDate == null && searchNumberDocument.trim().isEmpty()) {
            namedQueryInputDocument = "InputDocument.findAllID";
            queryCount = "SELECT count(i) from InputDocument i";
            return null;
        } else if(searchDate == null && !searchNumberDocument.trim().isEmpty()) {
            System.out.println("Buscando por numero");
            namedQueryInputDocument = "InputDocument.findAllIDNumber";
            queryCount = "SELECT count(i) from InputDocument i WHERE i.numberDocument = :number";
            param = new HashMap<>();
            param.put("number", searchNumberDocument.trim());
            return param;
        } else if(searchDate != null && searchNumberDocument.trim().isEmpty()) {
            System.out.println("Busncaod opor fecha");
            namedQueryInputDocument = "InputDocument.findAllIDDate";
            queryCount = "SELECT count(i) from InputDocument i WHERE DATE(i.inputDate) = DATE(:date)";
            param = new HashMap<>();
            param.put("date", searchDate);
            System.out.println(searchDate);
            return param;
        } else {
            System.out.println("Buscando por los 2");
            namedQueryInputDocument = "InputDocument.findAllIDDateNumber";
            queryCount = "SELECT count(i) from InputDocument i WHERE DATE(i.inputDate) = DATE(:date) AND i.numberDocument = :number";
            param = new HashMap<>();
            param.put("number", searchNumberDocument.trim());
            param.put("date", searchDate);
            return param;
        }
    }
    
    public InputDocument getInputDocument() {
        return inputDocument;
    }

    public void setInputDocument(InputDocument inputDocument) {
        this.inputDocument = inputDocument;
    }
    
    public Date getDate() {
        return today;
    }

    public String getSearchNumberDocument() {
        return searchNumberDocument;
    }

    public void setSearchNumberDocument(String searchNumberDocument) {
        this.searchNumberDocument = searchNumberDocument;
    }

    public Date getSearchDate() {
        return searchDate;
    }

    public void setSearchDate(Date searchDate) {
        this.searchDate = searchDate;
    }
    
}
